﻿module Enferno.Public.Web.Components.Models {
    export class BasketItemModel {
        public id: number;
        public name: string;
        public subHeader: string;
        public imageUrl: string;
        public productUrl: string;
        public quantity: number;
        public displayPrice: number;
        public originalPrice: number;
        public vatRate: number;
        public priceListId: number;
        public partNo: string;
        public unitOfMeasurement: string;
        public lineNo: number;
        public isEditable: boolean;
        public isDiscountable: boolean;

        constructor();
        constructor(serverObject: any);
        constructor(serverObject?: any) {
            if (serverObject != null) {
                this.createFromServerObject(serverObject);
            }
        }

        public static create(partNo: string, priceListId: number, quantity: number): BasketItemModel {
            var model = new BasketItemModel();
            model.partNo = partNo;
            model.priceListId = priceListId;
            model.quantity = quantity;

            return model;
        }

        public getPriceSum(): number {
            if (this.displayPrice != null && this.quantity != null)
                return this.displayPrice * this.quantity;
            return 0;
        }

        private createFromServerObject(serverObject: any) {
            this.id = serverObject.id;
            this.name = serverObject.name;
            this.subHeader = serverObject.subHeader;
            this.imageUrl = serverObject.imageUrl;
            this.productUrl = serverObject.productUrl;
            this.quantity = serverObject.quantity;
            this.displayPrice = serverObject.displayPrice;
            this.originalPrice = serverObject.originalPrice;
            this.vatRate = serverObject.vatRate;
            this.priceListId = serverObject.priceListId;
            this.partNo = serverObject.partNo;
            this.unitOfMeasurement = serverObject.unitOfMeasurement;
            this.lineNo = serverObject.lineNo;
            this.isEditable = serverObject.isEditable;
            this.isDiscountable = serverObject.isDiscountable;
        }

        public serialize(): any {
            var obj: any = {};
            obj.id = this.id;
            obj.name = this.name;
            obj.subHeader = this.subHeader;
            obj.imageUrl = this.imageUrl;
            obj.productUrl = this.productUrl;
            obj.quantity = this.quantity;
            obj.displayPrice = this.displayPrice;
            obj.originalPrice = this.originalPrice;
            obj.vatRate = this.vatRate;
            obj.priceListId = this.priceListId;
            obj.partNo = this.partNo;
            obj.unitOfMeasurement = this.unitOfMeasurement;
            obj.lineNo = this.lineNo;
            obj.isEditable = this.isEditable;
            obj.isDiscountable = this.isDiscountable;

            return obj;
        }
    }
} 